var equipVm = new Vue({
	el:'#equip-content',
	data: function() {
		return {
			tmbhError: '',
			tmbh: '',
			zcbhError: '',
			zcbh: '',
			zybbhError: '',
			zybbh: '',
			typeError: '',
			type: '',
			nameError: '',
			name: '',
			vendorError: '',
			vendor: '',
			modelError: '',
			model: '',
			departError: '',
			depart: '',
			departid: '',
			workshopError: '',
			workshop: '',
			workshopid: '',
			userError: '',
			user: '',
			contactError: '',
			contact: '',
			locationError: '',
			location: '',
			noteError: '',
			note: ''
		}
	},
	mounted: function (){
		var self = this;
		mui.init();
		mui.plusReady(function() {
			var thisPage = plus.webview.currentWebview();
			if(thisPage.type == "CODE128") {
				self.tmbh = thisPage.code;
			} else {
				self.zcbh = thisPage.code;
			}
			self.zybbh = thisPage.zybbh;
			// console.log(thisPage.type);
			// console.log(thisPage.code);
			// console.log(thisPage.zybbh);
			
			self.initLayout();
			self.initValidator();
			self.initDapertData();
			self.initWorkshopData();
			self.initBrandData();
			self.initEquipmentTypeData();
		})
	},
	methods:{
		initDapertData: function() {
			var self = this;
			this.departPicker = new mui.PopPicker();
			var url = config.requestBaseUrl + 'getSystemStandData';
			mui.ajax(url,{
			 	data:{
					type: 'depart'
			 	},
			 	dataType:'json',//服务器返回json格式数据
			 	type:'get',//HTTP请求类型
			 	timeout:3000,//超时时间设置为10秒；
			 	success:function(data){
					var departData = [];
					data.data.forEach(function(item, index) {
						departData.push({value: item.id, text: item.name})
					})
					self.departPicker.setData(departData);
			 	},
			 	error:function(xhr,type,errorThrown){
					mui.alert("出错了:" + errorThrown);
			 	}
			});
		},
		initWorkshopData: function() {
			var self = this;
			this.workshopPicker = new mui.PopPicker();
			var url = config.requestBaseUrl + 'getSystemStandData';
			mui.ajax(url,{
			 	data:{
					type: 'workshop'
			 	},
			 	dataType:'json',//服务器返回json格式数据
			 	type:'get',//HTTP请求类型
			 	timeout:3000,//超时时间设置为10秒；
			 	success:function(data){
					self.workshopSourceData = data.data;
			 	},
			 	error:function(xhr,type,errorThrown){
					mui.alert("出错了:" + errorThrown);
			 	}
			});
		},
		initValidator: function() {
			this.constraints = {
				tmbh: {
					presence: true
				},
				// zcbh: {
				// 	presence: true
				// },
				zybbh: {
					presence: true
				},
				type: {
					presence: true,
					length: {
						maximum: 10,
						message: "长度不能超过10"
					}
				},
				name: {
					presence: true,
					length: {
						maximum: 10,
						message: "长度不能超过10"
					}
				},
				vendor: {
					presence: true,
					length: {
						maximum: 10,
						message: "长度不能超过10"
					}
				},
				model: {
					length: {
						maximum: 10,
						message: "长度不能超过10"
					}
				},
				depart: {
					presence: true,
					length: {
						maximum: 10,
						message: "长度不能超过10"
					}
				},
				workshop: {
					length: {
						maximum: 10,
						message: "长度不能超过10"
					}
				},
				user: {
					presence: true,
					length: {
						maximum: 10,
						message: "长度不能超过10"
					}
				},
				contact: {
					length: {
						maximum: 13,
						message: "联系方式长度不能超过13"
					}
				},
				location: {
					presence: true,
					length: {
						maximum: 30,
						message: "长度不能超过30"
					}
				},
				note: {
					length: {
						maximum: 60,
						message: "备注长度不能超过60"
					}
				}
			};
			// 为空时的信息
			validate.validators.presence.message = "不能为空!";
		},
		initLayout:function() {
			mui('.mui-scroll-wrapper').scroll({
				scrollY: true, //是否竖向滚动
				scrollX: false, //是否横向滚动
				startX: 0, //初始化时滚动至x
				startY: 0, //初始化时滚动至y
				indicators: true, //是否显示滚动条
				deceleration: 0.0006, //阻尼系数,系数越小滑动越灵敏
				bounce: true //是否启用回弹
			})
		},
		toDepartPicker: function() {
			var self = this;
			this.clearErrorInfo('depart');
			this.departPicker.show(function (selectItems) {
				self.depart = selectItems[0].text;
				self.departid = selectItems[0].value;
				setTimeout(function() {
					self.workshopDataConst();
				}, 10);
			})
		},
		toWorkshopPicker:function() {
			var self = this;
			if(!this.depart) {
				mui.toast("请先选择部门!");
			} else {
				this.clearErrorInfo('workshop');
				this.workshopPicker.show(function (selectItems) {
					self.workshop = selectItems[0].text;
					self.workshopid = selectItems[0].value;
				})
			}
		},
		toBrandPicker: function() {
			var self = this;
			this.clearErrorInfo('vendor');
			this.brandPicker.show(function (selectItems) {
				self.vendor = selectItems[0].text;
			})
		},
		initBrandData: function() {
			var self= this;
			var url = config.requestBaseUrl + "getSystemStandData";
			this.brandPicker = new mui.PopPicker();
			mui.ajax(url,{
				data:{
					type: 'brand'
				},
				dataType:'json',//服务器返回json格式数据
				type:'get',//HTTP请求类型
				timeout:3000,//超时时间设置为10秒；
				success:function(data){
					if(data.isSuccess) {
						var brandData = [];
						data.data.forEach(function(item, index) {
							brandData.push({value: item.id, text: item.name})
						})
						self.brandPicker.setData(brandData);
					} else {
						mui.toast(data.msg);
					}
				},
				error:function(xhr,type,errorThrown){
					mui.toast("出错啦: " + errorThrown);
				}
			});
		},
		toEquipemntTypePicker: function() {
			var self = this;
			this.clearErrorInfo('name');
			this.equipemntTypePicker.show(function (selectItems) {
				self.name = selectItems[0].text;
			})
		},
		initEquipmentTypeData: function() {
			var self = this;
			var url = config.requestBaseUrl + 'getSystemStandData';
			this.equipemntTypePicker = new mui.PopPicker();
			mui.ajax(url,{
				data:{
					type: "equipmentType"
				},
				dataType:'json',//服务器返回json格式数据
				type:'get',//HTTP请求类型
				timeout:3000,//超时时间设置为10秒；
				success:function(data){
					if(data.isSuccess) {
						var equipmentTypeData = [];
						data.data.forEach(function(item, index) {
							equipmentTypeData.push({value: item.id, text: item.name})
						})
						self.equipemntTypePicker.setData(equipmentTypeData);
					} else {
						mui.toast(data.msg);
					}
				},
				error:function(xhr,type,errorThrown){
					mui.alert("出错了: " + errorThrown);
				}
			});
		},
		workshopDataConst: function() {
			var self = this;
			var workshopData = [];
			this.workshopSourceData.forEach(function(item, index) {
				if(item.departid == self.departid) {
					workshopData.push({value: item.id, text: item.name});
				}
			})
			this.workshopPicker.setData(workshopData);
		},
		openBarcode: function () {
			createWithoutTitle('barcode_scan.html', {
				titleNView:{
					type: 'float',
					backgroundColor: 'rgba(215,75,40,0.3)',
					titleText: '扫一扫',
					titleColor: '#FFFFFF',
					autoBackButton: true,
					buttons: [{
						fontSrc: '_www/helloh5.ttf',
						text: '\ue302',
						fontSize: '18px',
						onclick: 'javascript:scanPicture()'
					}]
				}
			});
		},
		scaned: function(t, r, f){
			if(t == "CODE128") {
				this.tmbh = r;
				this.tmbhError = false;
			} else {
				this.zcbh = r;
				this.zcbhError = false;
			}
		},
		clearErrorInfo: function(filed) {
			if(this[filed + 'Error'] && !this[filed]) {
				this[filed] = '';
				this[filed + 'Error'] = '';
			}
		},
		equipmentTypePicker: function() {
			var self = this;
			this.clearErrorInfo('type');
			var picker = new mui.PopPicker();
			picker.setData([{value:'电子设备',text:'电子设备'}]);
			picker.show(function (selectItems) {
				self.type = selectItems[0].text;
			})
		},
		submitEquipment: function() {
			var fileds = ['zcbh', 'tmbh', 'zybbh', 'type', 'name', 'vendor', 'model', 'depart', 'workshop', 'user', 'contact', 'location', 'note'];
				
			this.resetErrorMsg(fileds);
			var data = this.validateDataConstructor(fileds);
			var errors = validate(data, this.constraints);
			
			if(errors) {
				this.showError(errors, fileds);
			} else {
				// console.log("可以提交")
				this.submitData();
			}
		},
		validateDataConstructor: function(fileds) {
			var self = this;
			var data = {};
			fileds.forEach(function(item, index) {
				if(self[item]) {
					data[item] = self[item];
				}
			})
			return data;
		},
		showError: function(errors, fileds) {
			var self = this;
			fileds.forEach(function(item, index) {
				if(errors[item]){
					self[item + 'Error'] = errors[item];
				}
			})
		},
		resetErrorMsg: function(fileds) {
			var self = this;
			fileds.forEach(function(item, index) {
				if(item != 'zybbh' && self[item]){
					self[item + 'Error'] = '';
				}
			})
		},
		submitData: function() {
			var self = this;
			var url = config.requestBaseUrl + 'addNewEquipment';
			var recorderid = plus.storage.getItem("userid");
			var recorder = plus.storage.getItem("name");
			
			mui.ajax(url,{
				data:{
					zcbh: this.zcbh,
					tmbh: this.tmbh,
					zybbh: this.zybbh,
					type: this.type,
					name: this.name,
					vendor: this.vendor,
					model: this.model,
					depart: this.depart,
					departid: this.departid,
					workshop: this.workshop,
					workshopid: this.workshopid,
					user: this.user,
					contact: this.contact,
					location: this.location,
					note: this.note,
					recorderid: recorderid,
					recorder: recorder
				},
				dataType:'json',//服务器返回json格式数据
				type:'post',//HTTP请求类型
				timeout:10000,//超时时间设置为10秒；
				success:function(data){
					mui.toast(data.msg)
					if(data.isSuccess) {
						var indexPage = plus.webview.getLaunchWebview();
						mui.fire(indexPage,'refresh',{});
						
						var thispage = plus.webview.currentWebview();
						thispage.close();
					}
				},
				error:function(xhr,type,errorThrown){
					mui.toast("出错啦!请联系管理员...");
				}
			});
		}
	}
})

function scaned(t, r, f){
	equipVm.scaned(t, r, f);
}